package com.oracle.user.contrller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.oracle.role.entity.RoleEntity;
import com.oracle.user.entity.UsersEntity;
import com.oracle.user.feign.RoleFeignService;
import com.oracle.user.service.UsersService;
import com.oracle.user.vo.UserVo;
import com.oracle.util.Compents;
import com.oracle.util.PageUtils;
import com.oracle.util.R;
import org.apache.commons.collections4.MapUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 *
 * @author oracle
 * @email 2878992612@qq.com
 * @date 2024-10-31 09:37:51
 */
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private RoleFeignService roleFeignService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        //1、非空验证
        //空指针异常
//            只有两种情况    1、null调用 属性或方法 2、参数列表null
        //如果参数是必填项
//        String param_name = null;
//        if(!ObjectUtils.isEmpty(params)){
//            param_name = params.get("name")+"";
//        }
        //默认当前页
        Integer pageNum = 1;
        Integer pageSize = 10;

        //校验页码参数
        if(!ObjectUtils.isEmpty(params)){

            if(!ObjectUtils.isEmpty(params.get("pageNum"))) {
                pageNum = Integer.valueOf(params.get("pageNum") + "");
            }

            if(!ObjectUtils.isEmpty(params.get("pageSize"))){
                pageSize = Integer.valueOf(params.get("pageSize")+"");
            }
        }

        //2、查询用户信息
        PageUtils<UsersEntity> page = usersService.queryPage(params,pageNum,pageSize);

        //3、遍历用户 查询角色 feign
        List<UserVo> userVoList = new ArrayList<UserVo>();

        for (UsersEntity usersEntity : page.getRows()) {
            R info = roleFeignService.info(usersEntity.getRoleid());
            //某用户有脏数据 对应用户没有角色
            if(ObjectUtils.isEmpty(info)){
                continue;
            }

            //******
            //fegin 通过http 请求传输 传输的数据格式 统一是 LinkedHashMap
            // LinkedHashMap 的解决方案 两种
            //1、处理 linkedhashmap 转型问题
            RoleEntity role = JSON.parseObject(JSON.toJSONString(info.getData()), RoleEntity.class);

            //2、MapUtils
//            LinkedHashMap<String,Object> data = (LinkedHashMap<String,Object>)info.getData();
//            Integer id = MapUtils.getInteger(data, "id");
//            String rolename = MapUtils.getString(data, "rolename");

            //3.1 避免脏数据
//            if(ObjectUtils.isEmpty(role)){
//                return R.faild(Compents.ERR_MSG3);
//            }
            //3.2 将数据装入视图层对象
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(usersEntity,userVo);
            userVo.setRoleName(role.getRolename());
            //3.3 将封装好的对象装入到容器
            userVoList.add(userVo);
        }

        return R.ok(new PageUtils(page.getTotal(),userVoList));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userid}")
    public R info(@PathVariable("userid") Integer userid){
		UsersEntity users = usersService.getById(userid);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UsersEntity users){
		usersService.save(users);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UsersEntity users){
		usersService.updateById(users);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] userids){
		usersService.removeByIds(Arrays.asList(userids));

        return R.ok();
    }

}
